var {
  existContent,
  clickContent,
  randomUtils,
  textUtils,
  fmtUtils,
  timeUtils,
  appUtils,
  logUtils,
} = require ('../../utils/index');
var {Action} = require ('../../base/index');

var aShop = new Action ('逛街', {});
aShop.exec = function () {
  var content = '逛街赚钱';
  if (clickContent (content)) {
    sleep (randomUtils.getRandom (3, 4, 1000));
    var reg = /浏览\d{1,3}秒可领\d{1,5}金币/;
    var TextUI = textUtils.existsTextMatches (reg);
    if (TextUI) {
      var span = fmtUtils.getNumber (TextUI.text ());
      logUtils.info (span);
      this.shop (span);
    } else {
      logUtils.info ('没找到 /浏览\d{1,3}秒可领/ ');
      back ();
      sleep (randomUtils.getRandom (2, 4, 1000));
    }
  }
};
aShop.shop = function (span) {
  span = parseInt (span);
  span = randomUtils.getRandom (span, span + 10);
  logUtils.info (span);
  var hasExec = 0;
  var count = 0;
  while (true) {
    count++;
    var interval = randomUtils.getRandom (4, 8, 1000);
    sleep (interval);
    hasExec += 4;
    home ();
    sleep (randomUtils.getRandom (4, 8, 1000));
    appUtils.open ('抖音极速版');
    sleep (randomUtils.getRandom (3, 5, 1000));
    if (hasExec >= 30) {
      if (clickContent ('评价并收下')) {
        sleep (randomUtils.getRandom (2, 4, 1000));
        back ();
        sleep (randomUtils.getRandom (2, 4, 1000));
        break;
      }
    }
    if (count % 5 == 0) {
      console.log ('已经浏览了' + hasExec + '/' + span);
    }
    if (hasExec > span) {
      if (count % 5 != 0) {
        console.log ('已经浏览了' + hasExec + '/' + span);
      }
      if (clickContent ('评价并收下')) {
        sleep (randomUtils.getRandom (2, 4, 1000));
        back ();
        sleep (randomUtils.getRandom (2, 4, 1000));
      }
      break;
    }
  }
};
module.exports = aShop;
